#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include "caml.h"
#include "structures.h"
#include "playlist.h"

p_playlist		playlist_build(value		l)
{
  p_playlist		pl = NULL;
  char			*media_name;
  int			fd;

  pl = malloc(sizeof (struct s_playlist));

  printf ("pl_build\n");

  l = Field(l, 1);

  while (Is_block(l))
  {
    media_name = String_val(Field(l, 0));
    pl->media = malloc(sizeof (struct s_media));
    if (fd = fdopen(media_name, 'r'))
    {
      playlist_add(pl, fd, media_name);
      /* pl->media->name = media; */

      /* pl->nb_media ++; */
    }
    l = Field(l, 1);
  }

  return (pl);
}



p_playlist		playlist_add(p_playlist		pl,
				     int		fd,
				     char		*media_name)
{
  p_media			m;

  fd = NULL; /* grepme */

  m = malloc(sizeof (struct s_media));
  m->name = media_name;

  m->prev = pl->media->last;
  m->next = pl->media->last->next;
  pl->media = m;
  pl->media->last = m;
  pl->media->prev = m;
  pl->nb_media++;

  return (pl);
}
